export const setStore = (name, value) => {
    if (!name || !value || value === '') return;
    if (typeof value !== 'string') {
        value = JSON.stringify(value)
    }
    window.localStorage.setItem(name, value);
}

export const getStore = (name) => {
    if (!name) return;
    return window.localStorage.getItem(name)
}

export const removeStore = (name) => {
    if (!name) return;
    window.localStorage.removeItem(name)
}

export const checkNum = (num) => {

}

export const inArr = (arr, index) => {
    if (arr instanceof Array) {
        if (arr.indexOf(index) < 0) {
            return false
        }
        return true
    }
}

//设置cookie
export const setCookie = (name,value,day) =>{
    var date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires='+ date;
};
//获取cookie
export const getCookie = (name) => {
    var reg = RegExp(name+'=([^;]+)');
    var arr = document.cookie.match(reg);
    if(arr){
      return arr[1];
    }else{
      return '';
    }
};
//删除cookie
export const delCookie = (name) => {
    setCookie(name,null,-1);
};

//export const location = 'http://47.104.8.66:8080/resource/upload'
export const location = 'http://www.yuwubao.cn:8780/ts-upload/tsui/uploadFile'


// 获得图片
export function getObjectURL (file) {
    let url = null
    if (window.createObjectURL != undefined) { // basic
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) { // mozilla(firefox)
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) { // webkit or chrome
      url = window.webkitURL.createObjectURL(file)
    }
    return url
  }

//随机生成验证码
export function createCode() {
	let code = "";
	var codeLength = 5; //验证码的长度   
	var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
    's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
		'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数   
	for(var i = 0; i < codeLength; i++) { //循环操作   
		var charIndex = Math.floor(Math.random() * 36); //取得随机数的索引   
		code += random[charIndex]; //根据索引取得随机数加到code上   
    }
    return code
 }

 export function dateFormatter(date) {
    if (!date || date === undefined || date === '') {
        return ''
    }
    var date = new Date(date)
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    // var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    // var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    // var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D
 } 

 export function getYear(year) {
    let date = `${year}-11-11`
    var newDate = new Date(date)
    return newDate
 }

 export function filterObj(obj) {
    const newObj = {}
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            const element = obj[key];
            if (element && element !== undefined) {
                newObj[key] = element
            }
        }
    }
    console.log(newObj)
    return newObj
 }